//
//  HWPersonInformationViewController.m
//  嗨文
//
//  Created by 响当当 on 2019/1/16.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "HWPersonInformationViewController.h"
#import "HWPersonInformationCell.h"
#import "HWChooseView.h"
#import "HWLoginViewController.h"

@interface HWPersonInformationViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, strong)NSArray *personInformationArr;
@property (nonatomic, strong)NSArray *infoDateArr;
@property (nonatomic, strong)HWPersonalInformationModel *model;
@property (nonatomic, strong)NSIndexPath *indexPath;
@end

@implementation HWPersonInformationViewController

- (NSArray *)personInformationArr
{
    if (!_personInformationArr)
    {
        
        NSString *str = current_User.isHideinfo.integerValue != 0 ? @"账号" : @"支付宝提现账号";
        
        _personInformationArr = @[@"真实姓名",@"手机号",@"微信号",@"性别",@"阅读章节自动购买",str,@"切换账号",@"退出"];
    }
    return _personInformationArr;
}

- (NSArray *)infoDateArr
{
    if (!_infoDateArr)
    {
        _infoDateArr = [[NSArray alloc]init];
    }
    return _infoDateArr;
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,NAVIBAR_HEIGHT,SCREEN_WIDTH,SCREEN_HEIGHT-NAVIBAR_HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = 0;
        _tableView.backgroundColor = LCHexColor(0xF5F5F5);
    }
    return _tableView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setTitle:@"个人信息"];
    [self setDefaultNav];
    
    [self.view addSubview:self.tableView];
    [self getInfor];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self buryPoint];
}

#pragma mark - tableview Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.personInformationArr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 6 || indexPath.row == 7)
    {
        return 56*kUIScaleBasePlus;
    }
    else
    {
        return 46*kUIScaleBasePlus;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HWPersonInformationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"personInformationCell"];
    if (!cell)
    {
        cell = [[HWPersonInformationCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"personInformationCell"];
        cell.selectionStyle = 0;
    }
    
    if (indexPath.row == 3 || indexPath.row == 4)
    {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    if (self.infoDateArr.count)
    {
        if (indexPath.row == 3 || indexPath.row == 4)
        {
            NSNumber *num = self.infoDateArr[indexPath.row];
            NSString *str = [NSString stringWithFormat:@"%ld",num.integerValue];
            [cell getPersonInfoTitle:self.personInformationArr[indexPath.row] andSubTitle:str];
        }
        else
        {
            [cell getPersonInfoTitle:self.personInformationArr[indexPath.row] andSubTitle:self.infoDateArr[indexPath.row]];
        }
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HWChooseView *view = [[HWChooseView alloc]initWithFrame:self.view.bounds];
    WeakSelf(self)
    view.getDateBlock = ^(NSString *timeStr)
    {
        weakSelf.indexPath = indexPath;
        
        if (indexPath.row == 3)
        {
            [weakSelf updateCheapter:@"" Gender:[timeStr isEqualToString:@"男"] ? @"1" : @"0"];

        }
        else if (indexPath.row == 4)
        {
            [weakSelf updateCheapter:[timeStr isEqualToString:@"是"] ? @"1" : @"0" Gender:@""];
        }
    };
    if (indexPath.row == 3)
    {
        [self.view addSubview:view];
        [view getDate:@[@"男",@"女"]];
    }
    else if(indexPath.row == 4)
    {
        [self.view addSubview:view];
        [view getDate:@[@"是",@"否"]];
    }
    else if(indexPath.row == 6 || indexPath.row == 7)
    {
        [self logOut];
    }
    
}

#pragma mark - 获取个人信息
- (void)getInfor
{
    
    NSDictionary *prarmers = @{
                               @"access_token" : current_User.token                               };
    
    WeakSelf(self)
    [MyInfoManager personInfo:prarmers sucess:^(id  _Nonnull acceptData) {
        DISMISS_SVP(1.0f);
        HWPersonalInformationModel *model = [[HWPersonalInformationModel alloc]initWithDic:acceptData];
        weakSelf.model = model;
        weakSelf.infoDateArr = @[model.realName,model.phoneNum,model.wxaccount,model.gender ? [model.gender stringValue] : @"1",[model.automaticDeduction stringValue],model.alipayAccount,model.area,@"",@""];
        [weakSelf.tableView reloadData];
    } fail:^(NSError * _Nullable error) {
        
    }];
}

- (void)updateCheapter:(NSString *)cheap Gender:(NSString *)gender
{
    
    NSString *userid = [NSString stringWithFormat:@"%ld",self.model.userId.integerValue];
    
    NSDictionary *prarmers = @{
                               @"access_token" : current_User.token,
                               @"userId" : userid,
                               @"gender" : gender,
                               @"automaticDeduction" : cheap
                               };
    
    WeakSelf(self)
    [NetWorkingManager postRequestUrl:FULL_WEB_URL(KAPI_UPDATE) withPramater:prarmers sucess:^(id responseObject) {
        [weakSelf.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:weakSelf.indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
        HWPersonInformationCell *cell = [weakSelf.tableView cellForRowAtIndexPath:weakSelf.indexPath];
        if ([cheap isEqualToString:@""])
        {
            [cell getPersonInfoTitle:@"性别" andSubTitle:gender];
        }
        else if([gender isEqualToString:@""])
        {
            [cell getPersonInfoTitle:@"阅读章节自动购买" andSubTitle:cheap];
        }
        
    } failure:^(NSError *error) {
        
    }];
}

- (void)logOut
{
    NSDictionary *prarmers = @{
                               @"access_token" : current_User.token
                               };
    WeakSelf(self)
    [NetWorkingManager postRequestUrl:FULL_WEB_URL(KAPI_LOGOUT) withPramater:prarmers sucess:^(id responseObject) {
        HWLoginViewController *vc = [[HWLoginViewController alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        [[HWUtilties getCurrentVC].navigationController presentViewController:vc animated:YES completion:^{
            
            current_User.hasLogin = @"0";
            
            [current_User savaSessionSucess:^{
                AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
                [appDelegate gotoHome];
                [weakSelf.navigationController popViewControllerAnimated:YES];
                [HWUtilties removeCookie];
            } fail:^(NSError *error) {
                
            }];
            
            
        }];
    } failure:^(NSError *error) {
        
    }];
}

- (void)buryPoint
{
    NSString *lastStayTime = [kUserDefaults objectForKey:@"time"];
    NSString *nowStayTime = [NSString stringWithFormat:@"%ld",[[HWUtilties getNowTimeTimestamp3] integerValue] - lastStayTime.integerValue];
    
    [HWUtilties buryPointWithStayTime:nowStayTime
                            timestamp:[HWUtilties getNowTimeTimestamp3]
                             memberId:current_User.userID
                              fromUrl:nil
                                toUrl:@"/userInfo"
                               fromId:nil
                                 toId:@"e8459c53f31f97de"];
}
@end
